package com.zhiche.lisa.bms.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhiche.lisa.bms.pojo.dto.fee.FeeReceivableDTO;
import com.zhiche.lisa.bms.pojo.dto.price.OrderItemDTO;
import com.zhiche.lisa.bms.pojo.vo.util.OrderAssignVO;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class OmsUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(OmsUtil.class);

    @Value("${lisa.tms.url}")
    private String url;

    @Value("${lisa.tms.getOrderAssignByConsignCodeURL}")
    private String getOrderAssignByConsignCodeURL;

    @Value("${lisa.tms.getOrderItemByItemSourceKeyURL}")
    private String getOrderItemByItemSourceKeyURL;

    //@Value("${lisa.tms.getOrderByIdURL}")
    //private String getOrderByIdURL;

    //@Value("${lisa.tms.getLocationByIdUrl}")
    //private String getLocationByIdUrl;

    @Value("${lisa.tms.queryFeeReceivableOmsURL}")
    private String queryFeeReceivableOmsURL;

    @Value("${lisa.socketTimeout}")
    private Integer socketTimeout;

    public RestfulResponse<OrderAssignVO> getOrderAssignByConsignCode(String consignCode, String token) throws BaseException {
        if (!org.springframework.util.StringUtils.hasText(consignCode)) {
            throw new BaseException("委托单号[consignCode]，不能为空！");
        }
        if (!org.springframework.util.StringUtils.hasText(token)) {
            throw new BaseException("token鉴权，不能为空！");
        }
        // 发送请求给oms
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        String omsUrl = url + getOrderAssignByConsignCodeURL + "/" + consignCode;
        String strReturn = HttpClientUtil.get(omsUrl, headNamePairs, Lists.newArrayList(), socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<OrderAssignVO> shipTaskVO = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<OrderAssignVO>>() {
            });
            return shipTaskVO;
        } else {
            return null;
        }

    }

    public OrderItemDTO getOrderItemByItemSourceKey(String itemSourceKey) throws BaseException {
        List<NameValuePair> headNamePairs = Lists.newArrayList();
//        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        String omsUrl = url + getOrderItemByItemSourceKeyURL;
        List<NameValuePair> param = Lists.newArrayList();
        param.add(new BasicNameValuePair("itemSourceKey", itemSourceKey));
        String strReturn = HttpClientUtil.get(omsUrl, param, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            //   RestfulResponse<OrderItemDTO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<OrderItemDTO>>(){});

            RestfulResponse<OrderItemDTO> restfulResponse =
                    JSON.parseObject(strReturn, new TypeReference<RestfulResponse<OrderItemDTO>>() {
                    });
            return restfulResponse.getData();
        } else {
            return null;
        }
    }
    //public OrderDTO getOrderById (Long id, String token)throws BaseException{
    //    List<NameValuePair> headNamePairs = Lists.newArrayList();
    //    headNamePairs.add(new BasicNameValuePair("Authorization", token));
    //    String omsUrl = url + getOrderByIdURL  ;
    //    List<NameValuePair> param = Lists.newArrayList();
    //    param.add(new BasicNameValuePair("id",id.toString()));
    //    String strReturn = HttpClientUtil.get(omsUrl, headNamePairs, param, socketTimeout);
    //    if (StringUtils.isNotEmpty(strReturn)) {
    //        RestfulResponse<OrderDTO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<OrderDTO>>() {
    //        });
    //        return restfulResponse.getData();
    //    } else {
    //        return null;
    //    }
    //}

    /**
     * 根据订单明细ID从Oms获取应收账款信息
     *
     * @param wayBillDtlIds
     * @param token
     * @return
     */
    public Map<String, FeeReceivableDTO> queryFeeReceivableOms(List<String> wayBillDtlIds, String token) {
        Map<String, FeeReceivableDTO> map = Maps.newHashMap();
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        String parmsJson = JSON.toJSONString(wayBillDtlIds);
//         url + queryFeeReceivableOmsURL
        String strReturn = HttpClientUtil.postJson(url + queryFeeReceivableOmsURL, headNamePairs, parmsJson, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<Map<String, FeeReceivableDTO>> restfulResponse =
                    JSON.parseObject(strReturn, new TypeReference<RestfulResponse<Map<String, FeeReceivableDTO>>>() {
                    });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                map = restfulResponse.getData();
            }
        }
        return map;
    }
}
